package kr.saeildamil.androidforstudy2.ch27;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CopyPasteText extends Activity {
	EditText copyEdit;
	TextView pasteText;
	ClipboardManager mClipboard;

	Button btnSourceText;
	Button btnSourceIntent;
	Button btnTargetText;
	Button btnTargetIntent;

	Button lBtn;
	Button rBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch27_copytext);

		copyEdit = (EditText) findViewById(R.id.copyedit);
		pasteText = (TextView) findViewById(R.id.pastetext);
		mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

		btnSourceText = (Button) findViewById(R.id.source01);
		btnSourceIntent = (Button) findViewById(R.id.source02);
		btnSourceText.setOnLongClickListener(longListener);
		btnSourceIntent.setOnLongClickListener(longListener);

		btnTargetIntent = (Button) findViewById(R.id.targetintent);
		btnTargetIntent.setOnDragListener(mDragListener);

		lBtn = (Button) findViewById(R.id.lbtn);
		rBtn = (Button) findViewById(R.id.rbtn);

		lBtn.setOnLongClickListener(longListener);
		rBtn.setOnDragListener(mDragListener);
	}

	View.OnDragListener mDragListener = new OnDragListener() {

		@Override
		public boolean onDrag(View v, DragEvent event) {
			Button btn;
			if (v instanceof Button) {
				btn = (Button) v;
			} else {
				return false;
			}
			switch (event.getAction()) {
			case DragEvent.ACTION_DRAG_STARTED:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
					btn.setText("Drop, OK!");
				} else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
					btn.setText("Intent Drop OK!");
				} else {
					return false;
				}
				break;
				
			case DragEvent.ACTION_DRAG_ENTERED:
				btn.setText("Enter");
				break;
				
			case DragEvent.ACTION_DRAG_EXITED:
				btn.setText("Exit");
				break;
				
			case DragEvent.ACTION_DROP:
				if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
					String text = event.getClipData().getItemAt(0).getText().toString();
					btn.setText(text);
				} else if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
					Intent intent = event.getClipData().getItemAt(0).getIntent();
					startActivity(intent);
					btn.setText("Naver");
				}
				break;
				
			case DragEvent.ACTION_DRAG_ENDED:
				if (event.getResult()) {
					Toast.makeText(CopyPasteText.this, "Drag&Drop Completed", Toast.LENGTH_SHORT).show();
				} else {
					btn.setText("Target");
				}
				break;
			}
			return true;
		}
	};

	View.OnLongClickListener longListener = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			ClipData clip;
			switch (v.getId()) {
			case R.id.source01:
				clip = ClipData.newPlainText("dragText", "DragText");
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				break;
			case R.id.source02:
				clip = ClipData.newIntent("intent", new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com")));
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				break;
			case R.id.lbtn:
				clip = ClipData.newPlainText("dragText", "DragText");
				v.startDrag(clip, new CanvasShadow(v), null, 0);
			}
			return false;
		}
	};

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btncopy:
			copyText();
			break;
		case R.id.btnpaste:
			pasteText();
			break;
		case R.id.btncopyintent:
			copyIntent();
			break;
		case R.id.btnpasteintent:
			pasteIntent();
			break;
		}
	}

	private void copyText() {
		String str = copyEdit.getText().toString();
		if(str.length() != 0){
			ClipData clip = ClipData.newPlainText("text", str);
			mClipboard.setPrimaryClip(clip);
			Toast.makeText(this, "Text copied", Toast.LENGTH_SHORT).show();
		}
	}

	private void pasteText() {
		if (!mClipboard.hasPrimaryClip()) {
			Toast.makeText(this, "Clipboard is Empty!", Toast.LENGTH_SHORT).show();
			return;
		}
		if (!mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
			Toast.makeText(this, "Clipboard has no text", Toast.LENGTH_SHORT).show();
			return;
		}

		ClipData data = mClipboard.getPrimaryClip();
		ClipData.Item item = data.getItemAt(0);
		pasteText.setText(item.getText());
	}

	private void copyIntent() {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
		ClipData clip = ClipData.newIntent("intent", intent);
		mClipboard.setPrimaryClip(clip);
		Toast.makeText(this, "Intent copied", Toast.LENGTH_SHORT).show();
	}

	private void pasteIntent() {
		if (!mClipboard.hasPrimaryClip()) {
			Toast.makeText(this, "Clipboard is Empty!", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (!mClipboard.getPrimaryClipDescription().hasMimeType(
				ClipDescription.MIMETYPE_TEXT_INTENT)) {
			Toast.makeText(this, "Clipbarod has no intent!", Toast.LENGTH_SHORT)
					.show();
			return;
		}

		ClipData clip = mClipboard.getPrimaryClip();
		ClipData.Item item = clip.getItemAt(0);
		Intent intent = item.getIntent();
		if (intent != null) {
			startActivity(intent);
		}
	}

	class CanvasShadow extends View.DragShadowBuilder {
		int mWidth;
		int mHeight;
		Paint paint;
		Paint paint02;

		public CanvasShadow(View v) {
			super(v);
			mWidth = v.getWidth();
			mHeight = v.getHeight();
			init();
		}

		private void init() {
			paint = new Paint();
			paint.setColor(Color.CYAN);

			paint02 = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint02.setColor(Color.MAGENTA);
			paint02.setStrokeWidth(8);
			paint02.setStyle(Paint.Style.STROKE);
		}

		@Override
		public void onProvideShadowMetrics(Point shadowSize,
				Point shadowTouchPoint) {
			super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
			shadowSize.set(mWidth, mHeight);
			shadowTouchPoint.set(mWidth/3, mHeight/3);
		}

		@Override
		public void onDrawShadow(Canvas canvas) {
			super.onDrawShadow(canvas);
			canvas.drawRect(0, 0, mWidth, mHeight, paint);
			canvas.drawCircle(mWidth/2, mHeight/2, mHeight/2-5, paint02);
		}
	}
}
